/**
 * @project readnote
 * @Title BookServiceImpl.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.book.service.impl;

import java.util.List;

import com.cr.backbone.hibenate.AppBaseServiceImpl;
import com.readnote.book.dao.UserBookDAO;
import com.readnote.book.model.Book;
import com.readnote.book.model.UserBook;
import com.readnote.book.service.UserBookService;
import com.readnote.user.model.User;



public class UserBookServiceImpl extends AppBaseServiceImpl<UserBook> implements UserBookService {
	
	public UserBookDAO getDAO(){
		return ((UserBookDAO)(dao));
	}
	
	/**
	 * 用户添加书籍到我的书籍
	 * @param book
	 * @param user
	 */
	public void saveAddUserBook(Book book,User user){
		UserBook ub = null;
		//验证是否存在
		if(haveUserBook(book,user))
			return ;
		
		//不存在，添加到我的书籍
		ub = new UserBook();
		ub.setBook(book);
		ub.setUser(user);
		getDAO().save(ub);
	}
	
	/**
	 * 用户是否已经存在此书籍
	 * @param book
	 * @param user
	 * @return
	 */
	public boolean haveUserBook(Book book,User user){
		UserBook ub = null;
		//验证是否存在
		ub = getUserBookByBookAndUser(book, user);
		if(ub!=null &&!ub.isNullRow())
			return true;
		return false;
	}
	
	/**
	 * 获取单个用户书籍对象,by用户、书籍
	 * @param book
	 * @param user
	 * @return
	 */
	public UserBook getUserBookByBookAndUser(Book book,User user){
		return getDAO().getUserBookByBookAndUser(book,user);
	}
	
	/**
	 * 获取用户书籍集合，by用户
	 * @param user
	 * @return
	 */
	public List<UserBook> getListUserBookByUser(User user) {
		return getDAO().getListUserBookByUser(user);
	}
	
}
